﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using PhilipService;
using PhilipService.Entity;
using TCommon;

namespace PhilipSupport.Controls.MainControl
{
    public partial class ServiceDetail : UserControl
    {
        private long ServiceId;
        private GetServiceDetailResponse resp;
        private bool showBtn = false;

        public delegate void CloseServiceHandler(long serviceId);
        public event CloseServiceHandler ServiceClosed;

        public ServiceDetail(long serviceId, bool showBtn = false)
        {
            InitializeComponent();
            if (showBtn)
            {
                btn_finish.Visible = true;
                button_cancel.Visible = true;
                btn_finish.Enabled = false;
            }
            else
            {
                btn_finish.Visible = false;
                button_cancel.Visible = false;
            }
            this.showBtn = showBtn;
            this.ServiceId = serviceId;
            Task.Factory.StartNew(() =>
            {
                var manager = new RestServiceManager();
                resp = manager.GetServiceDetail(serviceId);
                this.Invoke(new Action(() =>
                {
                    label_username.Text = resp.Name;
                    label_devicename.Text = resp.DeviceName;
                    label_starttime.Text = resp.StartTime;
                    label_endtime.Text = resp.EndTime;
                    label_rating.Text = string.IsNullOrEmpty(resp.Rating) ? "" : AppConst.UserRating[resp.Rating];
                    PhilipContext.supportMain.UpdateTitle(string.Format("{0}的咨询详情", resp.Name));
                    if (showBtn)
                    {
                        btn_finish.Enabled = true;
                    }
                }));
            });
        }

        private void btn_viewuser_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(new Action(() =>
            {
                var manager = new RestServiceManager();
                var res = manager.GetUserInfo(resp.UserId, ServiceId);
                if (res != null) {
                    this.Invoke(new Action(() =>
                    {
                        var client = new ClientDetail(res);
                        PhilipContext.supportMain.AddControl(client);
                    }));                  
                }
            }));
            
        }

        private void btn_viewdevice_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                var manager = new RestServiceManager();
                var res = manager.GetDeviceInfo(resp.DeviceId, ServiceId);
                this.Invoke(new Action(() =>
                {
                    var device = new DeviceDetail(res);
                    PhilipContext.supportMain.AddControl(device);
                }));
            });    
        }

        private void panel_Paint(object sender, PaintEventArgs e)
        {
            var pen = new Pen(Color.LightGray);
            e.Graphics.DrawLine(pen, new Point(0, 0), new Point(this.Width, 0));
            e.Graphics.DrawLine(pen, new Point(panel.Width - 1, 0), new Point(panel.Width - 1, panel.Height - 1));
            e.Graphics.DrawLine(pen, new Point(0, panel.Height - 1), new Point(panel.Width - 1, panel.Height - 1));
            e.Graphics.DrawLine(pen, new Point(0, panel.Height - 1), new Point(0, 0));
        }

        private void btn_viewrecord_Click(object sender, EventArgs e)
        {
            var control = new RecordControl(ServiceId, resp.Note, this.showBtn);
            control.NoteChanged += Control_NoteChanged;
            PhilipContext.supportMain.AddControl(control);
        }

        private void Control_NoteChanged(string note)
        {
            resp.Note = note;
        }

        private void btn_finish_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                var manager = new RestServiceManager();
                int code = manager.CloseService(this.ServiceId);
                this.Invoke(new Action(() =>
                {
                    if (code != 0)
                    {
                        MessageBox.Show("关单失败，请重试", "关单失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        ServiceClosed?.Invoke(ServiceId);
                        PhilipContext.supportMain.RemoveControl();
                    }
                }));
                
            });
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            PhilipContext.supportMain.RemoveControl();
        }

        private void btn_viewnote_Click(object sender, EventArgs e)
        {
            var control = new UserComment(ServiceId, resp.EvaluateDetail);
            PhilipContext.supportMain.AddControl(control);
        }
    }
}
